import { Component, Input, OnInit } from '@angular/core';
import { SFSchema } from '@delon/form';
import { HttpClient } from '@angular/common/http';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';
import { FormBuilder, FormGroup } from '@angular/forms';
import { format } from 'date-fns'

@Component({
  selector: 'app-user-edit',
  templateUrl: './settime.component.html',
  styles: []
})
export class SettimeComponent implements OnInit {
  validateForm!: FormGroup;
  @Input()type: 0 | 1;
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
  };
  
  param: any;
  showForm = false;
  
  formSubmit() {
    
    let cardUsingEndTime = format(this.validateForm.value.rangePickerTime[1],'YYYY-MM-DD HH:mm:ss');
    let cardUsingStartTime = format(this.validateForm.value.rangePickerTime[0],'YYYY-MM-DD HH:mm:ss');
    
    console.log(new Date(cardUsingEndTime));
    const body: any = [{
      cardUsingEndTime: cardUsingEndTime,
      cardUsingStartTime: cardUsingStartTime,
      id: this.param.id,
    }];
    this.http.request<any>('PUT', '/core/door/card/batch',{body}).subscribe(res => {
      console.log(res)
      if (res.status === '200') {
        this.msg.success('操作成功');
        this.modalRef.destroy('ok');
      } else {
        this.msg.error(res.message)
      }
    })

  }
  constructor(
    private http: HttpClient,
    private msg: NzMessageService,
    private modalRef: NzModalRef,
    private fb: FormBuilder
  ) { }

  async ngOnInit() {
    this.showForm = true;
    this.validateForm = this.fb.group({
      rangePickerTime: [[new Date(this.param.cardUsingStartTime),new Date(this.param.cardUsingEndTime)]]
    });
  }

}
